const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teams: [],
    users: wx.getStorageSync('USERS')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '团队二维码',
    });
    var interval = setInterval(function () {
      if (that.data.users.id) {
        clearInterval(interval);
        that.onTeams();
      } else {
        that.setData({
          users: wx.getStorageSync('USERS')
        });
      }
    }, 1 * 10);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  onTeams: function () {
    var that = this;
    var parmas = {
      uid: that.data.users.id
    }
    util.wxPost({
      url: "/rest/lufei/teams", params: parmas,
      success: function (res) {
        util.hideToast();
        if (res.data.status == "000") {
          that.setData({
            teams: res.data.data
          });
        }
      }
    });
  },
  onBackTeams: function (options) {
    var that = this;
    var uid = that.data.users.id;
    var tid = options.target.dataset.tid;
    wx.showModal({
      title: '操作提示',
      content: "你即将退出当前团队?",
      success: function (e) {
        if (e.confirm) {
          var params = {
            "uid": uid,
            "tid": tid
          }
          util.wxPost({
            url: "/rest/lufei/backTeams", params: params,
            success: function (data) {
              var teamId = data.data.data.teamId;
              var users = that.data.users;
              users.teamId = teamId;
              wx.setStorage({
                key: 'USERS',
                data: users,
                success: function () {
                  wx.clearStorageSync();
                  wx.setStorageSync("USERSClean", "1");
                  wx.switchTab({
                    url: '/pages/index/index'
                  });
                }
              });
            }
          });
        }
      }
    });
  },
  onJoinTeams: function (options) {
    var that = this;
    wx.scanCode({
      success: function (res) {
        if (res.errMsg == "scanCode:ok") {
          var data = JSON.parse(res.result).data;
          var params = {
            "tid": data.id,
            "uid": that.data.users.id
          }
          util.wxPost({
            url: "/rest/lufei/joinTeams", params: params,
            success: function (res) {
              util.hideToast();
              if (res.data.status == "000") {
                var users = that.data.users;
                users.teamId = params.tid;
                wx.setStorage({
                  key: 'USERS',
                  data: users,
                  success: function () {
                    wx.clearStorageSync();
                    wx.setStorageSync("USERSClean", "1");
                    wx.switchTab({
                      url: '/pages/index/index'
                    });
                  }
                });
              } else {
                wx.showToast({
                  title: "加入团队失败!",
                  icon: "failure",
                  duration: 1500
                });
              }
            }
          });
        }
      }
    });
  }
})